﻿using Contrib.VimeoField.Settings;
using Contrib.VimeoField.ViewModels;
using JetBrains.Annotations;
using Orchard;
using Orchard.ContentManagement;
using Orchard.ContentManagement.Drivers;
using Orchard.ContentManagement.Handlers;
using Orchard.Localization;


namespace Contrib.VimeoField.Drivers
{
    [UsedImplicitly]
    public class VimeoFieldDriver : ContentFieldDriver<Fields.VimeoField>
    {
        
        public IOrchardServices Services { get; set; }

        private const string TemplateName = "Fields/Contrib.Vimeo";

        public VimeoFieldDriver(IOrchardServices services)
        {
            Services = services;
            T = NullLocalizer.Instance;
        }

        public Localizer T { get; set; }

        private static string GetPrefix(Fields.VimeoField field, ContentPart part)
        {
            return string.Format("{0}.{1}", part.PartDefinition.Name, field.Name).Replace(" ", "_");
        }

        private static string GetDifferentiator(Fields.VimeoField field)
        {
            return field.Name;
        }

        protected override DriverResult Display(ContentPart part, Fields.VimeoField field, string displayType,
            dynamic shapeHelper)
        {            
            var vm = new VimeoFieldViewModel(field);
            return ContentShape("Fields_Contrib_Vimeo", 
                s => 
                s.Name(field.Name)
                .VideoId(vm.VideoId)
                .Width(vm.Width)
                .Height(vm.Height)
                .Url(vm.Url)
                .Settings(field.PartFieldDefinition.Settings.GetModel<VimeoFieldSettings>())
                );
        }

        protected override DriverResult Editor(ContentPart part, Fields.VimeoField field, dynamic shapeHelper)
        {
            var vimeoFieldViewModel = BuildViewModel(field);
            return ContentShape("Fields_Contrib_Vimeo_Edit", GetDifferentiator(field),
                () => shapeHelper.EditorTemplate(TemplateName: "Fields.Contrib.Vimeo.Edit", Model: vimeoFieldViewModel, Prefix: GetPrefix(field, part)));
        }

        protected override DriverResult Editor(ContentPart part, Fields.VimeoField field, IUpdateModel updater, dynamic shapeHelper)
        {
            VimeoFieldViewModel vimeoFieldViewModel = BuildViewModel(field);
            if (updater.TryUpdateModel(vimeoFieldViewModel, GetPrefix(field, part), null, null))
            {
                vimeoFieldViewModel.UpdateField(field);
            }

            return Editor(part, field, shapeHelper);
        }
        protected VimeoFieldViewModel BuildViewModel(Fields.VimeoField vimeoField)
        {
            return new VimeoFieldViewModel(vimeoField);
        }
        

        
        protected override void Importing(ContentPart part, Fields.VimeoField field, Orchard.ContentManagement.Handlers.ImportContentContext context)
        {
            context.ImportAttribute(field.FieldDefinition.Name + "." + field.Name, "VideoId", v=> field.VideoId = v);
            context.ImportAttribute(field.FieldDefinition.Name + "." + field.Name, "Width", v => field.Width = Int32.Parse(v), () => field.Width = 500);            
            context.ImportAttribute(field.FieldDefinition.Name + "." + field.Name, "Height", v=>field.Height = Int32.Parse(v), () => field.Height = 281);
        }

        protected override void Exporting(ContentPart part, Fields.VimeoField field, ExportContentContext context)
        {
           context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("VideoId", field.VideoId);
           // don't export 0 width/heights if no video id has been entered
           if (!String.IsNullOrEmpty(field.VideoId))
           {
                context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Width", field.Width);
                context.Element(field.FieldDefinition.Name + "." + field.Name).SetAttributeValue("Height", field.Height);
           }
        }

    }
}